﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Presenters.Views;
using University.Workshop.Business;
using University.Workshop.Model;
using System.Data;
using System.Globalization;
using System.IO;

[assembly: CLSCompliant(true)]
namespace University.Workshop.Presenters
{
    public class ProductPresenter
    {
        string messageTrue = "Successful registration";
        string messageFalse = "The Product could not be registered";
        public IProductView View { get; set; }
        public ProductPresenter(IProductView view) 
        {
            this.View = view;
            LoadNBuyProduct();
        }
        public void LoadNBuyProduct()
        {
            this.View.NBuyProduct = ProductManager.LoadNBuyProduct();
        }

        public DataTable LoadProduct(string product)
        {
            return ProductManager.LoadProduct(product);
        }
        public void LoadNameProduct(string product)
        {
            DataTable dataProduct = ProductManager.LoadNameProduct(product);
            View.CodeProduct = int.Parse(dataProduct.Rows[0]["IdProduct"].ToString(),CultureInfo.InvariantCulture);
            View.StockCurrent = int.Parse(dataProduct.Rows[0]["StockCurrent"].ToString(),CultureInfo.InvariantCulture);            
        }
        public bool SaveRegistrationProduct() 
        {
            bool isvalid = false; 
            try
            {
                Product product = new Product();
                ProductBuy buy = new ProductBuy();
                product.IdProduct = this.View.CodeProduct;
                product.GroupProduct = this.View.GroupProduct;
                product.NameProduct = this.View.NameProduct;                               
                product.StockCurrent = this.View.StockCurrent+this.View.StockOrder;
                
                buy.IdProduct = this.View.CodeProduct;
                buy.CIManager = this.View.CIUser;
                buy.DateBuy = this.View.DateBuy;
                buy.Supplier = this.View.Supplier;
                buy.StockOrder = this.View.StockOrder;
                buy.UnitPrice = this.View.UnitPrice;
                buy.TotalPrice = this.View.TotalPrice;
                buy.ImageBuy = this.View.PictureProduct;
                buy.Description = this.View.Description;
                if ((ValidateFields.NameValidation(product.NameProduct)==null) && (ValidateFields.NameValidation(buy.Supplier)== null))
                {
                    if (ProductManager.NewProductRegister(product, buy))
                    {
                        View.Message = messageTrue;
                        isvalid = true;
                    }
                    else
                    {
                        View.Message = messageFalse;
                        isvalid = false;
                    }
                }
                else 
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw new IOException(ex.Message);
                //View.Message = "An unknown error occurred. ";
            }
            return isvalid;
        }

        public bool ModifyRegistrationProduct()
        {
            bool isvalid = false;
            try
            {
                Product product = new Product();
                ProductBuy buy = new ProductBuy();
                product.IdProduct = this.View.CodeProduct;
                product.GroupProduct = this.View.GroupProduct;
                product.NameProduct = this.View.NameProduct;
                product.StockCurrent = this.View.StockCurrent + this.View.StockOrder;

                buy.IdBuyProduct = this.View.NBuyProduct;
                //buy.IdProduct = this.View.CodeProduct;
                //buy.CIManager = this.View.CIUser;
                buy.DateBuy = this.View.DateBuy;
                buy.Supplier = this.View.Supplier;
                buy.StockOrder = this.View.StockOrder;
                buy.UnitPrice = this.View.UnitPrice;
                buy.TotalPrice = this.View.TotalPrice;
                buy.ImageBuy = this.View.PictureProduct;
                buy.Description = this.View.Description;
                if ((ValidateFields.NameValidation(product.NameProduct) == null) && (ValidateFields.NameValidation(buy.Supplier) == null))
                {
                    if (ProductManager.ModifyProductRegister(product, buy))
                    {
                        View.Message = messageTrue;
                        isvalid = true;
                    }
                    else
                    {
                        View.Message = messageFalse;
                        isvalid = false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw new IOException(ex.Message);
                //View.Message = "An unknown error occurred. ";
            }
            return isvalid;
        }
    }
}
